<template>
  <div>
    <header class="home-header wrap" :class="{'active' : headerScroll}">
      <router-link tag="i" to="./category"><i class="nbicon nbmenu2"></i></router-link>

      <div class="header-search">
        <span class="app-name"></span>
        <i class="iconfont icon-search"></i>
        <div class="tal_class_searchMask" @click="$router.push({ name: 'search' })"></div>
      </div>
      <router-link class="login" tag="span" to="./login" v-if="!isLogin">登录</router-link>
      <router-link class="login" tag="span" to="./user" v-else>
        <van-icon name="manager-o"/>
      </router-link>
    </header>
    <nav-bar></nav-bar>
    <swiper :list="swiperList"></swiper>
    <div class="category-list">
      <div v-for="item in categoryList" v-bind:key="item.categoryId">
        <img :src="item.imgUrl">
        <span>{{item.name}}</span>
      </div>
    </div>

    <div class="good">
      <header class="good-header">新品推荐</header>
      <div class="good-box">
        <div class="good-item" v-for="item in recommends" :key="item.goodsId" @click="goToDetail(item)">
          <img :src="prefix(item.goodsCoverImg)" alt="">
          <div class="good-desc">
            <div class="title">{{ item.goodsName }}</div>
            <div class="price">¥ {{ item.sellingPrice }}</div>
          </div>
        </div>
      </div>
    </div>

    <div class="good">
      <header class="good-header">热门商品</header>
      <div class="good-box">
        <div class="good-item" v-for="item in hots" :key="item.goodsId" @click="goToDetail(item)">
          <img :src="prefix(item.goodsCoverImg)" alt="">
          <div class="good-desc">
            <div class="title">{{ item.goodsName }}</div>
            <div class="price">¥ {{ item.sellingPrice }}</div>
          </div>
        </div>
      </div>
    </div>

    <div class="good" :style="{ paddingBottom: '100px'}">
      <header class="good-header">品牌商品</header>
      <header class="good-header">小米</header>
      <div class="good-box">
        <div class="good-item" v-for="item in newGoodses" :key="item.goodsId" @click="goToDetail(item)">
          <img :src="prefix(item.goodsCoverImg)" alt="">
          <div class="good-desc">
            <div class="title">{{ item.goodsName }}</div>
            <div class="price">¥ {{ item.sellingPrice }}</div>
          </div>
        </div>
      </div>
      <header class="good-header">OPPO</header>
      <div class="good-box">
        <div class="good-item" v-for="item in recommends2" :key="item.goodsId">
          <img :src="prefix(item.goodsCoverImg)" alt="">
          <div class="good-desc">
            <div class="title">{{ item.goodsName }}</div>
            <div class="price">¥ {{ item.sellingPrice }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import navBar from '@/components/NavBar'
  import swiper from '@/components/Swiper'
  import {getHome} from '../service/home'
  import {getUserInfo} from '../service/user'
  import {getLocal} from '@/common/js/utils'
  import {Toast} from 'vant'

  export default {
    name: 'home',
    data() {
      return {
        swiperList: [],
        isLogin: false,
        headerScroll: false,
        hots: [],
        newGoodses: [],
        recommends: [],
        recommends2: [
          {
            goodsName: 'OPPO FindX3',
            goodsCoverImg: '/goods-img/b6c3eea7-9d34-4ac0-ba66-2fde6f26253b.jpg',
            goodsId: 100001,
            sellingPrice:6999
          },
          {
            goodsName: 'OPPO Reno6 5G',
            goodsCoverImg: '/goods-img/bd8b2d93-c251-46b8-9990-77baaf3075f3.jpg',
            goodsId: 100002,
            sellingPrice:2799
          },
          {
            goodsName: 'OPPO Reno6 5G',
            goodsCoverImg: 'https://2b.zol-img.com.cn/product/207_320x240/637/cemZYvUa4lMTk.jpg',
            goodsId: 100003,
            sellingPrice:3599
          },
        ],
        categoryList: [],
      }
    },
    components: {
      navBar,
      swiper
    },
    async mounted() {
      const token = getLocal('token')
      if (token) {
        this.isLogin = true
      }
      window.addEventListener('scroll', this.pageScroll)
      Toast.loading({
        message: '加载中...',
        forbidClick: true
      });
      const {data} = await getHome()
      this.swiperList = data.carousels
      this.newGoodses = data.newGoodses
      this.hots = data.hotGoodses
      this.recommends = data.recommendGoodses
      Toast.clear()
    },
    methods: {
      pageScroll() {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
        scrollTop > 100 ? this.headerScroll = true : this.headerScroll = false
      },
      goToDetail(item) {
        this.$router.push({path: `product/${item.goodsId}`})
      }
    }
  }
</script>

<style lang="less" scoped>
  @import '../common/style/mixin';

  .home-header {
    position: fixed;
    left: 0;
    top: 0;
    .wh(100%, 50px);
    .fj();
    background-color: #126df5;
    line-height: 50px;
    padding: 0 15px;
    .boxSizing();
    font-size: 15px;
    color: #4e14a1;
    z-index: 10000;

    .nbmenu2 {
      color: @primary;
    }

    &.active {
      background: @primary;

      .nbmenu2 {
        color: #0f78d5;
      }

      .login {
        color: #0f78d5;
      }
    }

    .header-search {
      display: flex;
      .wh(74%, 20px);
      line-height: 20px;
      margin: 10px 0;
      padding: 5px 0;
      color: #232326;
      background: rgba(255, 255, 255, 0.7);
      border-radius: 20px;

      .app-name {
        padding: 0 10px;
        color: @primary;
        font-size: 20px;
        font-weight: bold;
        border-right: 1px solid #666;
      }

      .icon-search {
        padding: 0 10px;
        font-size: 17px;
      }

      .search-title {
        font-size: 12px;
        color: #666;
        line-height: 21px;
      }
    }

    .icon-iconyonghu {
      color: #fff;
      font-size: 20px;
    }

    .login {
      color: @primary;
      line-height: 52px;

      .van-icon-manager-o {
        font-size: 20px;
        vertical-align: -3px;
      }
    }
  }

  .category-list {
    background-color: #0dc3c3;
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    width: 100%;
    padding-bottom: 0px;

    div {
      display: flex;
      flex-direction: column;
      width: 20%;
      text-align: center;

      img {
        .wh(40px, 40px);
        margin: 13px auto 8px auto;
      }
    }
  }

  .good {
    .good-header {
      background: #f9f9f9;
      height: 50px;
      line-height: 50px;
      text-align: center;
      color: @primary;
      font-size: 16px;
      font-weight: 500;
    }

    .tal_class_searchBox {
      position: relative;
    }

    .tal_class_searchMask {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 9;
    }

    .good-box {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;

      .good-item {
        box-sizing: border-box;
        width: 50%;
        border-bottom: 1PX solid #e9e9e9;
        padding: 10px 10px;

        img {
          display: block;
          width: 120px;
          margin: 0 auto;
        }

        .good-desc {
          text-align: center;
          font-size: 14px;
          padding: 10px 0;

          .title {
            color: #222333;
          }

          .price {
            color: @primary;
          }
        }

        &:nth-child(2n + 1) {
          border-right: 1PX solid #e9e9e9;
        }
      }
    }
  }

  .floor-list {
    width: 100%;
    padding-bottom: 50px;

    .floor-head {
      width: 100%;
      height: 40px;
      background: #F6F6F6;
    }

    .floor-content {
      display: flex;
      flex-shrink: 0;
      flex-wrap: wrap;
      width: 100%;
      .boxSizing();

      .floor-category {
        width: 50%;
        padding: 10px;
        border-right: 1px solid #dcdcdc;
        border-bottom: 1px solid #dcdcdc;
        .boxSizing();

        &:nth-child(2n) {
          border-right: none;
        }

        p {
          font-size: 17px;
          color: #333;

          &:nth-child(2) {
            padding: 5px 0;
            font-size: 13px;
            color: @primary;
          }
        }

        .floor-products {
          .fj();
          width: 100%;

          img {
            .wh(65px, 65px);
          }
        }
      }
    }
  }
</style>
